home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / bin / foomatic-ppd-to-xml < prev    next >
Encoding:
Text File  |  2009-05-14  |  6.3 KB  |  186 lines

  1. #!/usr/bin/perl
  2. # -*- perl -*-
  3.  
  4. # Foomatic printer XML file generator to get XML files corresponding
  5. # to manufacturer-supplied PostScript PPDs (or also PPDs from driver
  6. # packages).
  7.  
  8. use Foomatic::Defaults;
  9. use Foomatic::DB;
  10. use Getopt::Std;
  11. use Data::Dumper;
  12. #use strict;
  13.  
  14. my $debug = 0;
  15.  
  16. # Program name
  17. $0 =~ m!/([^/]+)\s*$!;
  18. my $progname = ($1 || $0);
  19.  
  20. help() if !@ARGV;
  21. #my ($opt_h, $opt_d, $opt_p, $opt_A, $opt_P, $opt_w);
  22. getopts("d:r:p:lb:f:h");
  23. help() if $opt_h;
  24. my $drivers = $opt_d;
  25. my $rdriver = $opt_r;
  26. my $pdls = $opt_p;
  27. my $ppdlink = $opt_l;
  28. my $basedir = $opt_b;
  29. my $destdir = $opt_f;
  30.  
  31. $ppdfile = $ARGV[0];
  32.  
  33. my $ppddlpath;
  34. if ($basedir) {
  35.     $basedir =~ s:/+$::;
  36.     if (! -d $basedir) {
  37.     die ("PPD base directory $basedir does not exist!\n");
  38.     }
  39.     if (! -r $ppdfile) {
  40.     $ppddlpath = $ppdfile;
  41.     $ppdfile = $basedir . "/" . $ppdfile;
  42.     if (! -r $ppdfile) {
  43.         die ("Given PPD file not found, neither as $ppddlpath nor as $ppdfile!\n");
  44.     }
  45.     } else {
  46.     $ppdfile =~ m:$basedir/(.*)$:;
  47.     $ppddlpath = $1;
  48.     }
  49. } else {
  50.     if (! -r $ppdfile) {
  51.     die ("Given PPD file $ppdfile not found!\n");
  52.     }
  53.     $ppddlpath = $ppdfile;
  54. }
  55.  
  56. my $parameters = {
  57.     ($drivers ? ('drivers' => [split(',', $drivers)]) : ()),
  58.     ($rdriver ? ('recommendeddriver' => $rdriver) : ()),
  59.     ($pdls ? ('pdls' => [split(',', $pdls)]) : ()),
  60.     ($ppdlink ? ('ppdlink' => 1) : ()),
  61.     ($basedir ? ('basedir' => $basedir) : ()),
  62. };
  63.  
  64. my $db = Foomatic::DB->new();
  65. my $dat = ppdtoperl($ppdfile, $parameters);
  66. my $existing = ($db->find_printer("$dat->{'make'}|$dat->{'model'}", 4, 1))[0];
  67. my $d = $db->get_printer($existing) if $existing;
  68. my $result;
  69. if ($existing && !$d->{'noxmlentry'}) {
  70.     $db->{'dat'} = $d;
  71.     if (!defined($parameters->{'drivers'})) {
  72.     $parameters->{'drivers'} = [$dat->{'driver'}];
  73.     }
  74.     if (!defined($parameters->{'pdls'})) {
  75.     $parameters->{'pdls'} = [split(',', $dat->{'general_cmd'})];
  76.     } else {
  77.     push(@{$parameters->{'pdls'}}, split(',', $dat->{'general_cmd'}));
  78.     }
  79.     Foomatic::DB::apply_driver_and_pdl_info($db->{'dat'}, $parameters);
  80.     $db->{'dat'}{'general_ieee'} = $dat->{'general_ieee'} if
  81.     defined($dat->{'general_ieee'});
  82.     $db->{'dat'}{'general_mfg'} = $dat->{'general_mfg'} if
  83.     defined($dat->{'general_mfg'});
  84.     $db->{'dat'}{'general_mdl'} = $dat->{'general_mdl'} if
  85.     defined($dat->{'general_mdl'});
  86.     $db->{'dat'}{'general_des'} = $dat->{'general_des'} if
  87.     defined($dat->{'general_des'});
  88.     $db->{'dat'}{'general_cmd'} = $dat->{'general_cmd'} if
  89.     defined($dat->{'general_cmd'});
  90.     $db->{'dat'}{'comment'} .= "\n      <p>\n\n" . $dat->{'comment'};
  91.     my $xml1 = $db->perltoxml('p');
  92.     my $xml2 = $db->get_printer_xml($existing);
  93.     $xml2 =~ s/(<\/functionality>)/$1\n  <driver><\/driver>/s if 
  94.     $xml2 !~ /<driver>/;
  95.     $xml2 = transferregexp($xml1, $xml2,
  96.     '<driver>\S*<\/driver>');
  97.     $xml2 =~ s/(<\/driver>)/$1\n  <drivers>\n  <\/drivers>/s if 
  98.     $xml2 !~ /<drivers>/;
  99.     $xml2 = transferregexp($xml1, $xml2,
  100.     '<drivers>.*<\/drivers>');
  101.     $xml2 =~ s/(<\/(mechanism|url)>)/$1\n  <lang>\n  <\/lang>/s if 
  102.     $xml2 !~ /<lang>/;
  103.     $xml2 = transferregexp($xml1, $xml2,
  104.     '<lang>.*<\/lang>');
  105.     $xml2 =~ s/(<\/lang>)/$1\n  <autodetect>\n  <\/autodetect>/s if 
  106.     $xml2 !~ /<autodetect>/;
  107.     $xml2 =~ s/(<autodetect>)/$1\n    <general>\n    <\/general>/s if 
  108.     $xml2 !~ /<autodetect>[\s\n\r]*<general>/s;
  109.     $xml2 = transferregexp($xml1, $xml2,
  110.     '<autodetect>[\s\n\r]*<general>.*<\/general>');
  111.     $xml2 = transferregexp($xml1, $xml2,
  112.     '<\/drivers>[\s\n\r]*<comments>.*<\/comments>[\s\n\r]*<\/printer>');
  113.     $result = $xml2;
  114.     print "Modifying printer entry $db->{'dat'}{'id'}.xml ...\n";
  115. } else {
  116.     $db->{'dat'} = $dat;
  117.     $db->{'dat'}{'comment'} =
  118.     "      This database entry was automatically generated\n" .
  119.     "      from the PPD file for this printer.<p>\n\n" .
  120.     $db->{'dat'}{'comment'};
  121.     $db->{'dat'}{'functionality'} = "A";
  122.     $result = $db->perltoxml('p');
  123.     print "Creating new printer entry $db->{'dat'}{'id'}.xml ...\n";
  124. }
  125. open FILE, "> " . ($destdir ? $destdir . "/" : ()) . 
  126.     $db->{'dat'}{'id'} . ".xml" or
  127.     die "Cannot write file $db->{'dat'}{'id'}.xml!\n";
  128. print FILE $result;
  129. close FILE;
  130.  
  131. exit 0;
  132.  
  133. sub transferregexp {
  134.  
  135.     my ($src, $dest, $regexp) = @_;
  136.  
  137.     # This function copies the text fraction matching $regexp out of
  138.     # the first string, cuts the piece of the second matching $regexp
  139.     # out of the second string and replaces it by the piece copied
  140.     # from the first string. This is mainly for transfering XML
  141.     # sections from one XML file to another (strings can be
  142.     # multi-line) without needing to rewrite the unaffected parts of
  143.     # the XML file.
  144.  
  145.     $src =~ m/($regexp)/s;
  146.     my $totransfer = $1;
  147.     $dest =~ s/$regexp/$totransfer/s if $totransfer;
  148.     return $dest;
  149. }
  150.  
  151. sub help {
  152.     print <<HELP;
  153.  
  154. $progname <options> <ppdfile>
  155. $progname -h
  156.  
  157.  <ppdfile>      : PPD file for which a printer XML file should be created
  158.  -d <drivers>   : Comma-separated list of drivers with which the printer
  159.                   works. First driver is the one for which the PPD file is.
  160.                   If not otherwise stated by the "-r" options, this is also
  161.                   the recommended driver.
  162.  -r <driver>    : Recommended driver. Supply this option to specify another
  163.                   driver than the driver from the PPD/the first one from the
  164.                   "-d" argument as the recommended driver.
  165.  -p <pdls>      : Comma-separated list of known Page Description Languages
  166.                   (PDLs) which the printer supports. This will add all 
  167.                   suitable drivers to the XML entry. Currently supported are:
  168.                   Postscript, PCLXL, PCL6, PCL5e, PCL5c, PCL5, and PCL4.
  169.  -l             : Add a link to the PPD file to the driver entry in the
  170.                   XML file.
  171.  -b <directory> : Base directory for a relative link to the PPD. If the
  172.                   base directory is given, the link set via the -l option 
  173.                   is relative to this directory (and not relative to the
  174.                   current directory). With a base directory given the 
  175.                   <ppdfile> can also be given relative to this directory.
  176.  -f <directory> : Directory where the resulting XML file to write to. The
  177.                   name of the file will be the printer ID with the ".xml"
  178.                   extension.  
  179.  -h             : show help information
  180.  
  181.  
  182. HELP
  183.     exit 1;
  184.  
  185. }
  186.